package cn.wolfcode.luowowo.common.util;


import cn.wolfcode.luowowo.common.exception.LogicException;
import org.springframework.util.DigestUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AssertUtil {
    private AssertUtil(){}
    public static void hasLength(String text, String message) {
        if (!(text != null && !"".equals(text))) {
            throw new LogicException(message);
        }
    }

    public static void isEquals(String str1, String str2,String msg) {
        if(str1 == null || str2 == null){
            throw new LogicException(msg);
        }
        if(!str1.equals(str2)){
            throw new LogicException(msg);
        }
    }

    public static boolean isPhone(String phone) {
        String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$";
        if (phone.length() != 11) {
            return false;
        } else {
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(phone);
            boolean isMatch = m.matches();
            return isMatch;
        }
    }

    public static String getMd5(String password){
        String md501= DigestUtils.md5DigestAsHex(password.getBytes());
        String temp =md501.substring(0, 8);
        String md502 =DigestUtils.md5DigestAsHex((temp+md501).getBytes());
        return md502;
    }
}
